package com.example.wishstar.viewmodel;

import android.app.DatePickerDialog;
import android.content.Context;
import android.icu.util.Calendar;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.Toast;

import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;

import com.example.wishstar.MyApplication;
import com.example.wishstar.model.CommonDate;
import com.example.wishstar.model.NewMemory;
import com.example.wishstar.model.NewWish;
import com.example.wishstar.repository.AddWishRepository;

import java.time.LocalDate;

public class AddWishViewModel {
    private String TAG = "AddWishViewModel";
    private ObservableField<NewWish> newWishObservableField;
    private Context context;
    private AddWishRepository addWishRepository;

    private int userId;
    private LocalDate nowDate;
    private ChoiceWishIconCallBack choiceWishIconCallBack;
    private MutableLiveData<CommonDate> date;

    public interface ChoiceWishIconCallBack  {
        void callback(int wishIcon);
    }

    public void setChoiceWishIconCallBack(ChoiceWishIconCallBack choiceWishIconCallBack) {
        this.choiceWishIconCallBack = choiceWishIconCallBack;
    }

    public MutableLiveData<CommonDate> getDate() {
        if (date == null) {
            date = new MutableLiveData<CommonDate>();
            String year = "" + nowDate.getYear();
            String month = "" + nowDate.getMonthValue();
            String day = "" + nowDate.getDayOfMonth();
            date.setValue(new CommonDate(year, month, day));
        }
        return date;
    }

    public AddWishViewModel(Context context) {
        this.context = context;
        userId = MyApplication.userId;
        nowDate = LocalDate.now();
        NewWish newWish = new NewWish(userId, nowDate);

        newWishObservableField = new ObservableField<>();
        newWishObservableField.set(newWish);
        addWishRepository = new AddWishRepository(MyApplication.getApi());
    }

    public String getWishTitle() {
        return newWishObservableField.get().wishTitle;
    }

    public void setWishTitle(String wishTitle) {
        newWishObservableField.get().wishTitle = wishTitle;
    }

    public void choice1(View view) {
        newWishObservableField.get().wishIcon = 1;
        choiceWishIconCallBack.callback(1);
    }

    public void choice2(View view) {
        newWishObservableField.get().wishIcon = 2;
        choiceWishIconCallBack.callback(2);
    }
    public void choice3(View view) {
        newWishObservableField.get().wishIcon = 3;
        choiceWishIconCallBack.callback(3);
    }
    public void choice4(View view) {
        newWishObservableField.get().wishIcon = 4;
        choiceWishIconCallBack.callback(4);
    }
    public void choice5(View view) {
        newWishObservableField.get().wishIcon = 5;
        choiceWishIconCallBack.callback(5);
    }

    public void choice6(View view) {
        newWishObservableField.get().wishIcon = 6;
        choiceWishIconCallBack.callback(6);
    }

    public void choice7(View view) {
        newWishObservableField.get().wishIcon = 7;
        choiceWishIconCallBack.callback(7);
    }
    public void choice8(View view) {
        newWishObservableField.get().wishIcon = 8;
        choiceWishIconCallBack.callback(8);
    }
    public void choice9(View view) {
        newWishObservableField.get().wishIcon = 9;
        choiceWishIconCallBack.callback(9);
    }
    public void choice10(View view) {
        newWishObservableField.get().wishIcon = 10;
        choiceWishIconCallBack.callback(10);
    }

    public void choice11(View view) {
        newWishObservableField.get().wishIcon = 11;
        choiceWishIconCallBack.callback(11);
    }

    public void choice12(View view) {
        newWishObservableField.get().wishIcon = 12;
        choiceWishIconCallBack.callback(12);
    }
    public void choice13(View view) {
        newWishObservableField.get().wishIcon = 13;
        choiceWishIconCallBack.callback(13);
    }
    public void choice14(View view) {
        newWishObservableField.get().wishIcon = 14;
        choiceWishIconCallBack.callback(14);
    }
    public void choice15(View view) {
        newWishObservableField.get().wishIcon = 15;
        choiceWishIconCallBack.callback(15);
    }

    public void choice16(View view) {
        newWishObservableField.get().wishIcon = 16;
        choiceWishIconCallBack.callback(16);
    }

    public void post() {
        NewWish newWish = newWishObservableField.get();
        Log.e(TAG, newWish.toString());
        addWishRepository.addWish(newWish);
    }

    public void check() {
        NewWish newWish = newWishObservableField.get();
        if (newWish.wishDate.isBefore(nowDate)) {
            Toast.makeText(context, "注意目标完成日期是否设置为今日之后", Toast.LENGTH_SHORT).show();
        }
    }

    public void showDatePickerDialog(View view) {
        Calendar calendar=Calendar.getInstance();
        int themeResId = 3;
        new DatePickerDialog( context, themeResId,  new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                CommonDate commonDate = new CommonDate(String.valueOf(year), String.valueOf(month + 1), String.valueOf(dayOfMonth));
                date.setValue(commonDate);
                newWishObservableField.get().setWishDate(LocalDate.of(year, month + 1, dayOfMonth));
                check();
            }
        }
                ,calendar.get(Calendar.YEAR)
                ,calendar.get(Calendar.MONTH)
                ,calendar.get(Calendar.DAY_OF_MONTH)).show();
    }
}
